﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BusinnessLogic;

namespace scrumbench
{
    public partial class ManageProductsForm : Form
    {
        public ManageProductsForm()
        {
            InitializeComponent();
        }

        public ManageProductsForm(int userid, string role)
        {
            InitializeComponent();

            this.userid = userid;
            this.role = role;
        }


        private string role;

        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        private int userid;

        public int Userid
        {
            get { return userid; }
            set { userid = value; }
        }

        private void ManageProductsForm_Load(object sender, EventArgs e)
        {
            //Disable button if Modify Products if not Administrator
            if (this.role != "Administrator")
            {

                this.modprod.Enabled = false;
            }

            
            BusinnessLogic.Userproduct BLuserprod = new BusinnessLogic.Userproduct();
            List<Product> userproductlist = BLuserprod.PopulateUserproductlist(userid);

            foreach (Product prod in userproductlist)
            {

                    
                    comboBox1.Items.Add(prod);



            }

            comboBox1.SelectedIndex = 2;

        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            LVwSprintlist.Items.Clear();
            #region Populatelist

            Product selectedprod = (Product)comboBox1.SelectedItem;

            //BusinnessLogic.Userproduct BLuserprod = new BusinnessLogic.Userproduct();
            //List<UserProduct> userproductlist = BLuserprod.PopulateUserproductlist(userid); 
            
            //foreach(UserProduct prodid in userproductlist)
            //{
            //    BusinnessLogic.ManageProducts BLprodmanager2 = new BusinnessLogic.ManageProducts();
            //    List<Product> productlist = BLprodmanager2.PopulateProductlist(prodid.ProductId);
            //    foreach(Product prod in productlist)
            //    {
            //        while (prod.name == comboBox1.Text)
            //        {
                        BusinnessLogic.ManageProducts BLprodmanager = new BusinnessLogic.ManageProducts();
                        List<Sprint> sprintlist = BLprodmanager.PopulateSprintlist(selectedprod.productID);

                        foreach (Sprint sprnt in sprintlist)
                        {

                            ListViewItem lvitem = new ListViewItem();
                            lvitem.Text = sprnt.SprintId.ToString();

                            ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                            nsubitem.Text = sprnt.Titel;

                            lvitem.SubItems.Add(nsubitem);

                            this.LVwSprintlist.Items.Add(lvitem);
                        }
                        //prod.name = null;
                //    }
                //}
            //}
            #endregion
        }

        private void LVwSprintlist_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            bool itemselected = false;

            if (LVwSprintlist.SelectedItems != null)
            {
                itemselected = true;
            }

            if (itemselected)
            {
                
                int sprintid = int.Parse(LVwSprintlist.FocusedItem.Text.ToString());
                string sprinttitel = LVwSprintlist.FocusedItem.SubItems[1].Text;

                SprintviewForm nsprntviewform = new SprintviewForm(sprintid, sprinttitel);
                nsprntviewform.Show();
                
                
            }
        }  

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void modprod_Click(object sender, EventArgs e)
        {
            AddProductForm tempForm = new AddProductForm(userid);
            tempForm.Show();

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
